/*
 * ResourceUtil.java Created on Aug 15, 2003 11:05:00 AM
 *
 * Copyright (C) 2003 eRocX Software Group - www.erocx.com
 */
package com.erocx.c24;

import java.util.*;
import java.net.URL;

/**
 * Title:        ResourceUtil
 * Description:  Just for my leisure time.
 * 
 * @author		<a href="mailto:eroclu@gmail.com">Andy Lu</a>
 * @version   	1.0
 */

public final class ResourceUtil {
	private static final String DEFAULT_BN = "com.erocx.c24.resource";
    private static final String IMAGE_ROOT = "com/erocx/c24/images/";
    private static HashMap bundles = new HashMap();
    private static Locale myLocale = Locale.CHINA; //Locale.getDefault();

    public static String getString(String resName) {
        return getString(DEFAULT_BN, resName);
    }

    public static String getString(String baseName, String resName) {
        try {
            if (baseName != null) {
                ResourceBundle bundle = (ResourceBundle) bundles.get(baseName);
                if (bundle == null) {
                    bundle = ResourceBundle.getBundle(baseName, myLocale);
                    bundles.put(baseName, bundle);
                }
                return bundle.getString(resName);
            }
        } catch (MissingResourceException mre) {
            mre.printStackTrace();
        }

        return null;
	}

    public static URL getImage(String imageName) {
        return getImage(null, imageName);
    }

    public static URL getCardImage(String imageName) {
        return getImage("playcards", imageName);
    }

    public static URL getImage(String subPath, String imageName) {
        if (subPath == null)
            subPath = "";

        if (subPath.length() > 0)
            subPath += "/";

        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        URL url = loader.getResource(IMAGE_ROOT + subPath + imageName);

        return url;
    }
}